import * as api from '@/api/system/settingManage'

const settings = {
  state: {
    settings: {}
  },
  mutations: {
    SET_SETTINGS: (state, settings) => {
      state.settings = settings
    }
  },
  actions: {
    fetchSettings({ commit }) {
      return new Promise((resolve, reject) => {
        api.getObj().then(response => {
          if (response.success) {
            const data = response.data.rows
            const settings = {}
            for (let item of data) {
              settings[item.key] = item.value
            }
            commit('SET_SETTINGS', settings)
            resolve(data)
          } else {
            reject(new Error(response.message))
          }
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default settings
